﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 用户管理系统
{
    public partial class UserManage : Form
    {
        //public int index = 0;
        public ConfigInfo configInfo = new ConfigInfo();
        public UserData userData = new UserData();
        private readonly Dictionary<string, int> colInfos = new Dictionary<string, int>()
        {
            { "类 型",100 },
            { "品牌 & 型号",250 },
            { "数量",50 },
            { "保 修",130 },
            { "备 注",100 },
        };
        public UserManage()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            InitWindow();
            UpdateDgvData();
        }


        /// <summary>
        /// 初始化窗口
        /// </summary>
        private void InitWindow()
        {
            if (userData == null)
            {
                this.Text = $"用户详细信息[新增用户]";
            }
            else
            {
                this.Text = $"用户详细信息[{userData.用户名称}]";
            }

            MaximizeBox = false;

            //提交按钮
            //this.btn_Submit.Location = new Point(Width - btn_Submit.Width - 30, btn_Add.Top);

            //信息显示框设置只读
            lbl_UserInfoShow.Font = configInfo.MyDgv.UserInfoShow.Font;
            lbl_UserInfoShow.ForeColor = configInfo.MyDgv.UserInfoShow.ForeColor;
            lbl_UserInfoShow.BackColor = configInfo.MyDgv.UserInfoShow.BackColor;
            lbl_UserCfgShow.Font = configInfo.MyDgv.UserCfgShow.Font;
            lbl_UserCfgShow.ForeColor = configInfo.MyDgv.UserCfgShow.ForeColor;
            lbl_UserCfgShow.BackColor=configInfo.MyDgv.UserCfgShow.BackColor;

            //设置标题列样式
            this.dgvData.EnableHeadersVisualStyles = false;
            this.dgvData.ColumnHeadersDefaultCellStyle.ForeColor = configInfo.MyDgv.SeconTableHeader.ForeColor;
            this.dgvData.ColumnHeadersDefaultCellStyle.BackColor = configInfo.MyDgv.SeconTableHeader.BackColor;
            this.dgvData.ColumnHeadersDefaultCellStyle.Font = configInfo.MyDgv.SeconTableHeader.Font;
            // 标题行高
            int rowHeight = MyClass.GetHeight(configInfo.MyDgv.MainTableText.Font.Height, 1.2);
            dgvData.RowTemplate.Height = rowHeight;

            //禁用行标题
            //this.dgvData.RowHeadersVisible = false;
            //禁用行标题大小调整
            this.dgvData.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
            //禁用列标题大小调整
            //this.dgvData.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //设置各表头各列文字居中
            this.dgvData.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            //设置隔行背景颜色
            //this.dgvData.AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray;
            this.dgvData.AlternatingRowsDefaultCellStyle.BackColor = (Color)new ColorConverter().ConvertFrom("#F2F2F2");
            //禁止增加新行
            this.dgvData.AllowUserToAddRows = false;
            //禁用列排序
            //dgvData.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;

            // 禁止用户改变DataGridView的所有列的列宽   
            //dgvData.AllowUserToResizeColumns = false;

            //禁止用户改变DataGridView所有行的行高   
            dgvData.AllowUserToResizeRows = false;
            //禁止修改ID列
            //dgvData.Columns["ID"].ReadOnly = true;
            dgvData.RowHeadersWidth = 30;

            //表格字体和颜色
            this.dgvData.DefaultCellStyle.SelectionForeColor = configInfo.MyDgv.SelectText.ForeColor;
            this.dgvData.DefaultCellStyle.SelectionBackColor = configInfo.MyDgv.SelectText.BackColor;
            this.dgvData.Font = configInfo.MyDgv.SeconTableText.Font;
            this.dgvData.DefaultCellStyle.ForeColor = configInfo.MyDgv.SeconTableText.ForeColor;
            this.dgvData.DefaultCellStyle.BackColor = configInfo.MyDgv.SeconTableText.BackColor;

            // 选择初始单元格
            //tbx_user.Select();
            //tbx_user.Focus();
            this.dgvData.Select();
            //this.dgvData.CurrentCell = dgvData[1, 0];

            //价格设置
            txt_Money.Font = configInfo.MyDgv.UserMoney.Font;
            txt_Money.ForeColor = configInfo.MyDgv.UserMoney.ForeColor;
            txt_Money.BackColor = configInfo.MyDgv.UserMoney.BackColor;

            //创建表
            InitDataTable();
        }

        /// <summary>
        /// 生成dgvData各列
        /// </summary>
        private void InitDataTable()
        {
            DataGridViewTextBoxColumn textColumn;
            string[] leftCols = { "品牌 & 型号", "备 注" };

            foreach (var colInfo in colInfos)
            {
                textColumn = new DataGridViewTextBoxColumn
                {

                    ValueType = typeof(string),
                    HeaderText = colInfo.Key,
                    SortMode = DataGridViewColumnSortMode.NotSortable,
                    Width = colInfo.Value,
                    MinimumWidth = colInfo.Value,
                    Name = colInfo.Key,
                };
                
                if (leftCols.Contains(colInfo.Key))
                {
                    textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
                }
                else
                {
                    textColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                }
                dgvData.Columns.Add(textColumn);
            }
            //dgvData.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dgvData.EditMode = DataGridViewEditMode.EditProgrammatically;
            dgvData.Rows.Add();
            Font tableFont = configInfo.MyDgv.SeconTableText.Font;
            this.dgvData.Columns[0].DefaultCellStyle.Font = new Font(tableFont.Name, tableFont.Size, FontStyle.Bold);

        }

        private void UpdateDgvData()
        {
            if (userData != null)
            {
                txt_Number.Text = userData.编号;
                txt_Name.Text = userData.用户名称;
                dtp_Date.Value = userData.购买日期;
                txt_Tel.Text = userData.联系电话;
                txt_Saleser.Text = userData.销售人员;
                txt_Installer.Text = userData.装机人员;
                txt_Money.Text = userData.金额?.ToString("F2");
                int i = 0;
                dgvData.Rows.Clear();
                foreach (var item in userData.配置)
                {
                    Object[] row = new object[]
                    {
                    item.类型,
                    item.名称,
                    item.数量,
                    item.保修,
                    item.备注,
                    };
                    dgvData.Rows.Add(row);
                    //dgvData.Rows[i].HeaderCell.Value = i+1;
                    i++;
                }
            }
            long addTime = (long)Math.Round((DateTime.Now - dtp_Date.Value).TotalDays,2);
            UpdateAddDate(addTime);

            txt_Number.Focus();
        }

        /// <summary>
        /// 用户资料控件文本改变事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserInfo_TextChanged(object sender, EventArgs e)
        {
            if (sender == dtp_Date)
            {
                lbl_UserInfoShow.Text = ((DateTimePicker)sender).Text;
            }
            else
            {
                lbl_UserInfoShow.Text= ((TextBox)sender).Text;
            }
        }

        /// <summary>
        /// 用户资料控件激活事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserInfo_Enter(object sender, EventArgs e)
        {
            if (sender == dtp_Date)
            {
                lbl_UserInfoShow.Text = ((DateTimePicker)sender).Text;
            }
            else
            {
                lbl_UserInfoShow.Text = ((TextBox)sender).Text;
                ((TextBox)sender).BackColor = configInfo.MyDgv.SelectText.BackColor;
            }
        }

        /// <summary>
        /// 打印过程
        /// </summary>
        private void PrintProc()
        {
            SetControlPro(false);
            MyPrint myPrint = new MyPrint(configInfo, userData, chk_BackImage.Checked);
            //myPrint.SetPrinter();
            //myPrint.SetPage();

            //myPrint.Print();
            myPrint.PreView();


            SetControlPro(true);
        }


        /// <summary>
        /// 设置控件状态
        /// </summary>
        /// <param name="status"></param>
        private void SetControlPro(bool status)
        {
            foreach (Control item in this.Controls)
            {
                item.Enabled = status;
            }
        }
        /// <summary>
        /// 用户资料控件激活事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserInfo_Leave(object sender, EventArgs e)
        {
            if (sender != dtp_Date)
            {
                ((TextBox)sender).BackColor = default;

            }
        }

        private void UpdateAddDate(long value)
        {
            //已购买时间位置
            lbl_AddDate.Text = $"已购买：{value}天";
            lbl_AddDate.Location = new Point(dgvData.Left + dgvData.Width - lbl_AddDate.Width, dgvData.Top + dgvData.Height + 14);
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {

        }

        private void btn_Print_Click(object sender, EventArgs e)
        {
            PrintProc();
        }

        private void dgvData_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvData.SelectedCells.Count == 1)
                lbl_UserCfgShow.Text = dgvData.SelectedCells[0].Value.ToString();
        }
    }
}
